package com.cloud.screen.jobhandler;

import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.cloud.api.domain.screen.VehicleStatisticsData;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.cloud.screen.config.WebSocketServer;
import com.cloud.screen.service.IVehicleMessageDailyService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * @author 杨斌
 * 2024-03-14/3/2024
 */
@Component
@Slf4j
public class StatisticsVehicle {

    @Resource
    private IVehicleMessageDailyService vehicleMessageDailyService;

    private final ObjectMapper objectMapper = new ObjectMapper();
    @JobExecutor(name = "vehicle_statistics_data")
    public void getVehicleStatistics(){

            List<VehicleStatisticsData> vehicleStatistics = vehicleMessageDailyService.getVehicleStatistics();

            try {
                String jsonString = objectMapper.writeValueAsString(Collections.singletonMap("vehicleStatistics", vehicleStatistics));
                WebSocketServer.sendMessageToAllUser(jsonString);
            } catch (JsonProcessingException e) {
                log.error("json 解析错误");
            } catch (IOException e) {
                log.error("websocket 发送错误");
            }

        }
}
